﻿using DrawIO45.Models.DrawIOs;
using DrawIO45.Models.Logic;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DrawIO45.Draws
{
    /// <summary>
    /// 画集成关系
    /// </summary>
    public class DrawExtend : IDrawOnRoot
    {
        DrawExtendOption Option;

        public DrawExtend() 
        {
            Option = new DrawExtendOption();
        }

        public DrawExtend(DrawExtendOption option)
        {
            Option = option;
        }

        public void DrawOnRoot(Root root, List<DILocationInfo<LClassInfo>> classInfos)
        {
            //// 画继承关系线
            var types = classInfos.Select(n => n.Object.Source).ToArray();
            foreach (var itemType in types)
            {
                if (Option.IgnoreInterface) 
                {
                    if (itemType.IsInterface)
                        continue;
                }

                var parent = itemType.BaseType;
                if (parent == null)
                    continue;

                var currentLInfo = classInfos.FirstOrDefault(n => n.Object.Source == itemType);
                if (currentLInfo == null)
                    continue;

                if (DrawHelper.CanFindCellByLClass(root, currentLInfo.Object) != true)
                    continue;

                var parentLInfo = classInfos.FirstOrDefault(n => n.Object.Source == parent);
                if (parentLInfo == null)
                    continue;

                if (DrawHelper.CanFindCellByLClass(root, parentLInfo.Object) != true)
                    continue;

                root.MxCells.Add(new MxCell
                {
                    Id = Guid.NewGuid().ToString(),
                    Parent = "1",
                    Style = "endArrow=block;dashed=1;endFill=0;endSize=12;html=1;rounded=0;",
                    Edge = "1",
                    Source = currentLInfo.Object.Id,
                    Target = parentLInfo.Object.Id,
                    Value = "extend",
                    MxGeometries = new List<MxGeometry>
                    {
                        new MxGeometry
                        {
                            Width = "160",
                            Relative = "1",
                            As = "geometry",
                        },
                    },
                });
            }
        }
    }

    public class DrawExtendOption 
    {
        /// <summary>
        /// 忽略接口之间的继承关系
        /// </summary>
        public bool IgnoreInterface { get; set; }
    }
}
